package com.basic.datapersist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger; 

import com.basic.common.PagerTool;
 

public abstract class BaseRowsSet implements NeedConnect{
	private static Logger logger = Logger.getLogger ( BaseRowsSet.class ) ;
	 
	private DbConnectTool connect = null;
	private DbBasicService tmpService = null;
	private DbBasicService dbTransService = null;
	protected ConditionTool conditionTool = new ConditionTool();
	public void useTransaction(DbBasicService dbService){
		this.dbTransService = dbService;
		this.dbTransService.setMapTable(getMapTable());
	}
	
	protected DbBasicService getDbService(){
		if(dbTransService != null){
			dbTransService.setMapTable(getMapTable());
			return dbTransService;
		}
		tmpService = DbFactory.instance(connect, DbBasicService.class);
		tmpService.setMapTable(getMapTable());
		return tmpService;
	}
	protected void freeDbService() throws Exception{
		if(tmpService != null){
			tmpService.commit();
			tmpService.freeResource();
			tmpService = null;
		}
	}
	
	@Override
	public void initConnect(DbConnectTool connect){
		this.connect = connect;
	}
	
	public void addOrderAsc(String columnName){
		conditionTool.addOrderAsc(columnName);
	}
	public void addOrderDesc(String columnName){
		conditionTool.addOrderDesc(columnName);
	}
	
	public void clearCondition(){
		conditionTool.clearCondition();
	}
	
	public void removeCodition(String column){
		conditionTool.removeCondition(column);
	}
	
	public void setPagerDto(PagerTool var) {
		conditionTool.queryLimit(var.getStartIndex(), var.getPageSize());
 	}
	
	public long queryCount() throws Exception{
		try {
			DbBasicService dbservice = getDbService();
			dbservice.setConditionTool(conditionTool);
			return dbservice.queryExecCount();
		} finally{
			freeDbService();
		}
		
	}
	
	protected List queryRows(Class<? extends OneRowDataInit> clazz) throws Exception{
		try {
			DbBasicService dbservice = getDbService();
			dbservice.setConditionTool(conditionTool);
			List<HashMap<String, String>> list = dbservice.queryExec();
			List<OneRowDataInit> relist = new ArrayList<OneRowDataInit>();
			for(HashMap<String, String> map : list){
				OneRowDataInit row = DbFactory.instance(connect, clazz);
				row.putData(map);
				relist.add(row);
			}
			return relist;
		} finally{
			freeDbService();
		}
	}
	
	public void updateExec(HashMap<String, String> newData) throws Exception{
		try {
			DbBasicService dbservice = getDbService();
			dbservice.setConditionTool(conditionTool);
			dbservice.setNewData(newData);
			dbservice.updateExec(); 
		} finally{
			freeDbService();
		}
	}
	
	public void deleteExec() throws Exception{
		try {
			DbBasicService dbservice = getDbService();
			dbservice.setConditionTool(conditionTool);
			dbservice.deleteExec();
		} finally{
			freeDbService();
		}
	}
	
	protected abstract MapTable getMapTable() ;
}
